package com.tozed.android.launcher.simpleui;

import com.tozed.android.launcher.R;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.widget.TextView;

public class SimpleBubbleTextView extends TextView
{
	private static final String TAG = "SimpleBubbleTextView";
	
	private static final float ADJUST_FONT_SIZE = 5.0F;
	private static final int FLAG_UPDATE_UNREAD = 4112;
	public static final int PRESS_RUNABLE_DELAY_TIME = 200;
	private static final int UPDATE_UNREAD_DELAY = 100;
	
	private Drawable mFolderBgDrawable; // 文件夹背景
	private boolean mHasUnreadMark;		// 是否有未读标志
	private Drawable mInfoDrawble;
	private int mInfoDrawbleHeight;
	private int mInfoDrawbleWidth;
	private int mInfoPadding;
	private Drawable mOnTouchDrawble;
	
	private Drawable mRemoveDrawable;
	private int mRemoveDrawableHeight;
	private int mRemoveDrawableWidth;
	private Paint mUnreadMarkPaint;
	private int mUnreadNum;
	private float mUnreadTextSize;
	private float mUnreadTextSmallSize;
	
	private Context mContext;
	
	private Handler mPostDrawHandler = new Handler(){
		public void handleMessage(Message msg){
			if (FLAG_UPDATE_UNREAD == msg.what)
				invalidate();
			
			super.handleMessage(msg);
		}
	};
	
	public Runnable mPressRunable = new Runnable(){
		public void run(){
			setPressed(true);
			postInvalidate();
		}
	};
	
	public SimpleBubbleTextView(Context context){
		this(context, null);
	}

	public SimpleBubbleTextView(Context context, AttributeSet attrs){
		this(context, attrs, 0);
	}

	public SimpleBubbleTextView(Context context, AttributeSet attrs, int defStyle){
		super(context, attrs, defStyle);
		
		mContext = context;
	    Resources res = mContext.getResources();
	    
	    mInfoDrawble = res.getDrawable(R.drawable.ic_number_bg_1);
	    mRemoveDrawable = res.getDrawable(R.drawable.ic_remove);
	    mRemoveDrawableWidth = mRemoveDrawable.getIntrinsicWidth();
	    mRemoveDrawableHeight = mRemoveDrawable.getIntrinsicHeight();
	    mInfoDrawbleWidth = mInfoDrawble.getIntrinsicWidth();
	    mInfoDrawbleHeight = mInfoDrawble.getIntrinsicHeight();
	    mInfoDrawble.setBounds(0, 0, mInfoDrawbleWidth, mInfoDrawbleHeight);
	    mRemoveDrawable.setBounds(0, 0, mRemoveDrawableWidth, mRemoveDrawableHeight);
	    
	    mInfoPadding = res.getDimensionPixelSize(R.dimen.simple_unread_info_padding);
	    mUnreadMarkPaint = new Paint();
	    mUnreadTextSize = res.getDimension(R.dimen.simple_unread_text_size);
	    mUnreadTextSmallSize = res.getDimension(R.dimen.simple_unread_textsize_small);
	    mUnreadMarkPaint.setTextSize(mUnreadTextSize);
	    mUnreadMarkPaint.setColor(Color.WHITE);
	    mUnreadMarkPaint.setTextAlign(Paint.Align.CENTER);
	    mUnreadMarkPaint.setFakeBoldText(true);
	    mOnTouchDrawble = res.getDrawable(R.drawable.bg_pressed);
	    
	    setDrawingCacheEnabled(true);
	    mFolderBgDrawable = res.getDrawable(R.drawable.bg_icon_pressed);
	}
	
	private void drawBubbleOutline(Canvas canvas)
	{
		int width = getWidth();
		int heiht = getHeight();
		
		canvas.save();
		canvas.translate(mScrollX, mScrollY);
		mOnTouchDrawble.setBounds(0, 0, width, heiht);
		mOnTouchDrawble.draw(canvas);
		canvas.restore();
	}
	
	private void drawBubbleOutlineInFolder(Canvas canvas)
	{
		int width = getWidth();
		int heiht = getHeight();
		
		canvas.save();
		canvas.translate(mScrollX, mScrollY);
		mFolderBgDrawable.setBounds(0, 0, width, heiht);
		mFolderBgDrawable.draw(canvas);
		canvas.restore();
	}
	
	// 画删除标志
	private void drawRemoveMark(Canvas canvas, int padding)
	{
		canvas.save();
		canvas.translate(mInfoPadding + mScrollX, mInfoPadding + mScrollY);
		mRemoveDrawable.draw(canvas);
		canvas.restore();
	}
	
	// 画未读标志
	private void drawUnreadNum(Canvas canvas, int width)
	{
	    int nNumber = getUnreadNum();
	    if ((mContext instanceof SimpleFolder) && (nNumber > 0))
	    {
	      Bitmap bmp = SimpleUtilities.getNum(mContext, nNumber);
	      if (bmp != null){
	        int diff = width - bmp.getWidth();
	        canvas.save();
	        canvas.drawBitmap(bmp, diff + mScrollX, mScrollY, mUnreadMarkPaint);
	        canvas.restore();
	      }else{
	    	  Log.e(TAG, "drawUnreadNum: The bitmap returned from method of Utilities.getNum is null. ");
	    	  return;
	      }
	    }
	    
	    if ((mContext instanceof SimpleUILauncher) && mHasUnreadMark && nNumber > 0){
		    int infoPadding = mInfoPadding;
		    int infoWidth = mInfoDrawbleWidth;
		    int infoHeight = mInfoDrawbleHeight;
		    int dx = width - infoWidth - mInfoPadding;
		    
		    canvas.save();
		    canvas.translate(dx + mScrollX, infoPadding + mScrollY);
		    mInfoDrawble.draw(canvas);
		    
		    String strNumber = null;
		    if (nNumber > 0 && nNumber <= 99){
		    	strNumber = String.valueOf(nNumber);
		    	mUnreadMarkPaint.setTextSize(mUnreadTextSize);
		    }else{
		    	strNumber = "99+";
			    mUnreadMarkPaint.setTextSize(mUnreadTextSmallSize);
		    }
		    
		    // 画未读文本数字
		    canvas.drawText(strNumber, infoWidth >> 1, 5.0F + ((infoHeight >> 1) + (int)(mUnreadTextSize / 4.0F)), mUnreadMarkPaint);
	    	canvas.restore();
	    }
	}
	
	public void applyFolderIconFromBubbleTextView(SimpleApplicationInfo appInfo, SimpleIconCache iconCache)
	{
		setCompoundDrawablesWithIntrinsicBounds(null, new FastBitmapDrawable(appInfo.getIcon(iconCache)), null, null);
		
		setText(appInfo.title);
		setTag(appInfo);
		
		setGravity(Gravity.CENTER_HORIZONTAL);
	}
	
	public boolean getUnreadMarkable(){
		return mHasUnreadMark;
	}

	public int getUnreadNum(){
		return mUnreadNum;
	}

	@Override
	protected void onDraw(Canvas canvas){
		super.onDraw(canvas);
		
		int width = getWidth();
		if (isPressed())
			drawBubbleOutlineInFolder(canvas);
		
		drawUnreadNum(canvas, width);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event)
	{
		// TODO: 需再测试
		
	    switch (event.getAction())
	    {
	    case MotionEvent.ACTION_MOVE:
	    case MotionEvent.ACTION_DOWN:
	      mPostDrawHandler.postDelayed(mPressRunable, 200);
	      return super.onTouchEvent(event);
	      
	    case MotionEvent.ACTION_UP: 
	    case MotionEvent.ACTION_CANCEL:
	    	break;
	    	
	    default:
		      return super.onTouchEvent(event);
	    }
	    
	    if (!isPressed())
	      setPressed(false);
	    
	    mPostDrawHandler.removeCallbacks(mPressRunable);
	    postInvalidate();
	    
	    return super.onTouchEvent(event);
	}
	
	public void setUnreadMarkable(boolean unreadMark){
		mHasUnreadMark = unreadMark;
	}

	public void setUnreadNum(int nUnreadNum)
	{
		mUnreadNum = nUnreadNum;
		if (mPostDrawHandler.hasMessages(FLAG_UPDATE_UNREAD))
			mPostDrawHandler.removeMessages(FLAG_UPDATE_UNREAD);
		
		mPostDrawHandler.sendEmptyMessageDelayed(FLAG_UPDATE_UNREAD, 0);
	}
}
